﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassEntities;
using System.Collections;

namespace Edu_Examination
{
    public partial class UC_XemDiemThi : UserControl
    {
        private tblSinhVien Session;

        public UC_XemDiemThi(tblSinhVien Session)
        {
            // TODO: Complete member initialization
            this.Session = Session;
            InitializeComponent();
            loadData();
        }
        DataTable tblDangKyLop;
        DataTable tblKetQuaThi;

        void loadData()
        {
            Hashtable hash = new Hashtable();
            hash["idSV"] = Session.Stt;
            tblDangKyLop = Lib.Mapping.ToDataTable(Lib.doConnection.SuperFacade.gettblDangKyLop(hash));
            loadLopHoc();
            tblKetQuaThi = Lib.Mapping.ToDataTable(Lib.doConnection.SuperFacade.gettblKetQuaThi(Session.Stt));
            if (tblKetQuaThi != null && tblKetQuaThi.Rows.Count > 0)
            {
                lvlDiemDetail.Items.Clear();
                foreach (DataRow dr in tblKetQuaThi.Rows)
                {
                    string xet = "Đang xử lý";
                    ListViewItem lvitem = new ListViewItem(dr["tenMH"].ToString());
                    string diem = dr["diemthi"].ToString();
                    if (!string.IsNullOrEmpty(diem)) xet = "Đã xong";
                    lvitem.SubItems.Add(xet);
                    lvitem.SubItems.Add(dr["diemdat"].ToString());
                    lvitem.SubItems.Add(diem);
                    string kq = "";
                    if (dr["ketqua"].ToString() == "True") kq = "Đậu";
                    else if (dr["ketqua"].ToString() == "False") kq = "Rớt";
                    lvitem.SubItems.Add(kq);
                    string temp = ((DateTime)dr["ngaythi"]).ToString("dd/MM/yyyy");
                    lvitem.SubItems.Add(temp);
                    lvlDiemDetail.Items.Add(lvitem);
                }
            }
        }
        void loadLopHoc()
        {
            lvlLopDaDK.Items.Clear();
            if (tblDangKyLop != null && tblDangKyLop.Rows.Count > 0)
            {
                foreach (DataRow row in tblDangKyLop.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = string.IsNullOrEmpty(row["Ten"].ToString()) ? "Không tên" : row["Ten"].ToString();
                    item.Tag = row["IdLH"].ToString();
                    lvlLopDaDK.Items.Add(item);
                }
            }
        }
        void loadDiem(string select)
        {
            if (tblKetQuaThi != null && tblKetQuaThi.Rows.Count > 0)
            {
                lvlDiemDetail.Items.Clear();
                DataRow[] rows = tblKetQuaThi.Select(select);
                foreach (DataRow dr in rows)
                {
                    string xet = "Đang xử lý";
                    ListViewItem lvitem = new ListViewItem(dr["tenMH"].ToString());
                    string diem = dr["diemthi"].ToString();
                    if (!string.IsNullOrEmpty(diem)) xet = "Đã xong";
                    lvitem.SubItems.Add(xet);
                    lvitem.SubItems.Add(dr["diemdat"].ToString());
                    lvitem.SubItems.Add(diem);
                    lvitem.Tag = ((DateTime)dr["ngaythi"]).ToString("dd/MM/yyyy");
                    string kq = "";
                    if (dr["ketqua"].ToString() == "True") kq = "Đậu";
                    else if (dr["ketqua"].ToString() == "False") kq = "Rớt";
                    lvitem.SubItems.Add(kq);
                    lvlDiemDetail.Items.Add(lvitem);
                }
            }
        }

        private void lvlLopDaDK_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lvlLopDaDK.GetItemAt(e.X, e.Y);
            if (item != null)
            {
                string select = "IdLopHoc = '" + item.Tag.ToString() + "'";
                loadDiem(select);
            }
        }

        private void btTim_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string ten = txtTenMon.EditValue == null ? "" : txtTenMon.EditValue.ToString();
            string select = "TenMH Like '%" + ten + "%'";
            if (txtTuNgay.EditValue == null || txtDenNgay.EditValue == null)
            {
                txtTuNgay.EditValue = null;
                txtDenNgay.EditValue = null;
                select = "TenMH Like '%" + ten + "%'";
            }
            else
            {
                DateTime tungay = (DateTime)txtTuNgay.EditValue;
                DateTime denngay = (DateTime)txtDenNgay.EditValue;
                select = "TenMH Like '%" + ten + "%' AND Ngaythi > '" + tungay + "' AND Ngaythi < '" + denngay + "'";
            }
            loadDiem(select);
        }
    }
}
